package com.example.administrator.managesystem.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.bean.GoodsListBean;
import com.example.administrator.managesystem.utils.GlobalConfig;
import com.example.administrator.managesystem.view.PickLandDialog;
import com.example.administrator.managesystem.view.SmoothCheckBox;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class MyRecyclerViewAdapter  extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
    private Context context;
    private List<GoodsListBean.DataBean> list;
    private boolean isVisble;


    public void setVisble(boolean visble) {
        isVisble = visble;
        notifyDataSetChanged();
    }

    public MyRecyclerViewAdapter(Context context,List<GoodsListBean.DataBean> list) {
        this.context=context;
        this.list=list;

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.goods_recycler_item, parent,
                false));
        return holder;
    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if(isVisble){
            holder.show_checkbox.setVisibility(View.VISIBLE);
           // holder.cb.setChecked(false);
            holder.show_checkbox.setSelected(false);
        }else{
            holder.show_checkbox.setVisibility(View.GONE);
        }
        Picasso.with(context).load(GlobalConfig.ONLINE_HTTP+""+list.get(position).getImage()).into(holder.goods_icon);
        holder.title.setText(list.get(position).getTitle());
        holder.series.setText(""+list.get(position).getSale_id());
        holder.storage.setText(list.get(position).getStock()+"千克");
        holder.price.setText(""+list.get(position).getPrice()+"元");
        holder.show_checkbox.setSelected(list.get(position).isCheck());
        holder.show_checkbox.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
//          if (list.get(position).isCheck()) {
//              list.get(position).setCheck(false);
//
//          } else {
//              list.get(position).setCheck(true);
//          }

          checkedListener.OnCheckedClick(v,list.get(position).isCheck(),position);

      }
  });


        Log.e("msg","list.get(position).isCheck():"+list.get(position).isCheck());


        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               editListener.onEditClick(v,list.get(position).getSale_id());
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        //CheckBox cb;
        LinearLayout show_checkbox;
        TextView title,series,storage,price,edit;
        ImageView goods_icon;
        public MyViewHolder(View view) {
            super(view);
           // cb = view.findViewById(R.id.goods_item_rb);
            title=view.findViewById(R.id.goods_name);
           series=view.findViewById(R.id.goods_series);
           storage=view.findViewById(R.id.goods_storage);
           price=view.findViewById(R.id.price_sell);
           edit=view.findViewById(R.id.edit_goods);
           goods_icon=view.findViewById(R.id.land_img);
           show_checkbox=view.findViewById(R.id.show_checkbox);

        }
    }

    //点击三角
    private OnEditClickListener editListener;
    public interface OnEditClickListener {
        void onEditClick(View view,int id);
    }
    public void setOnEditClickListener (OnEditClickListener listener) {
        editListener=listener;
    }


    private OnCheckedClickListener checkedListener;
    public interface OnCheckedClickListener {
        void OnCheckedClick(View view,boolean isCheck,int position);
    }
    public void setOnCheckedClickListener (OnCheckedClickListener listener) {
        checkedListener=listener;
    }

}
